package config

import (
	"yunj/pkg/yunj/core/dto"
	"yunj/pkg/yunj/util"
)

// 图像文件格式规则
var ImageExtRules = map[string]dto.FileExtRule{
	// 图像文件
	"jpg": {
		MIMETypes:  []string{"image/jpeg"},
		MagicBytes: [][]byte{{0xFF, 0xD8, 0xFF}}, // JPEG
	},
	"jpeg": {
		MIMETypes:  []string{"image/jpeg"},
		MagicBytes: [][]byte{{0xFF, 0xD8, 0xFF}}, // JPEG
	},
	"png": {
		MIMETypes:  []string{"image/png"},
		MagicBytes: [][]byte{{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}}, // PNG
	},
	"gif": {
		MIMETypes: []string{"image/gif"},
		MagicBytes: [][]byte{
			{0x47, 0x49, 0x46, 0x38, 0x37, 0x61}, // GIF87a
			{0x47, 0x49, 0x46, 0x38, 0x39, 0x61}, // GIF89a
		},
	},
	"bmp": {
		MIMETypes:  []string{"image/bmp"},
		MagicBytes: [][]byte{{0x42, 0x4D}}, // BMP
	},
	"webp": {
		MIMETypes:  []string{"image/webp"},
		MagicBytes: [][]byte{{0x52, 0x49, 0x46, 0x46}}, // RIFF header
	},
	"svg": {
		MIMETypes: []string{"image/svg+xml"},
		MagicBytes: [][]byte{
			{0x3C, 0x3F, 0x78, 0x6D, 0x6C}, // <?xml
			{0x3C, 0x73, 0x76, 0x67},       // <svg
		},
	},
	"ico": {
		MIMETypes:  []string{"image/x-icon", "image/vnd.microsoft.icon"},
		MagicBytes: [][]byte{{0x00, 0x00, 0x01, 0x00}}, // ICO
	},
}

// 其他文件格式规则
var FileExtRules = map[string]dto.FileExtRule{
	// 文档文件
	"pdf": {
		MIMETypes:  []string{"application/pdf"},
		MagicBytes: [][]byte{{0x25, 0x50, 0x44, 0x46, 0x2D}}, // PDF
	},
	"txt": {
		MIMETypes:  []string{"text/plain"},
		MagicBytes: nil, // 纯文本没有固定魔数
	},
	"rtf": {
		MIMETypes:  []string{"application/rtf", "text/rtf"},
		MagicBytes: [][]byte{{0x7B, 0x5C, 0x72, 0x74, 0x66, 0x31}}, // {\rtf1
	},
	"csv": {
		MIMETypes:  []string{"text/csv", "text/comma-separated-values"},
		MagicBytes: nil, // CSV没有固定魔数，通常通过扩展名识别
	},

	// Microsoft Office 文件
	"doc": {
		MIMETypes:  []string{"application/msword"},
		MagicBytes: [][]byte{{0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1}}, // OLE2
	},
	"xls": {
		MIMETypes:  []string{"application/vnd.ms-excel"},
		MagicBytes: [][]byte{{0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1}}, // OLE2
	},
	"ppt": {
		MIMETypes:  []string{"application/vnd.ms-powerpoint"},
		MagicBytes: [][]byte{{0xD0, 0xCF, 0x11, 0xE0, 0xA1, 0xB1, 0x1A, 0xE1}}, // OLE2
	},

	// Office Open XML 文件 (ZIP-based)
	"docx": {
		MIMETypes:  []string{"application/zip", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
		MagicBytes: [][]byte{{0x50, 0x4B, 0x03, 0x04}}, // ZIP
	},
	"xlsx": {
		MIMETypes:  []string{"application/zip", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
		MagicBytes: [][]byte{{0x50, 0x4B, 0x03, 0x04}}, // ZIP
	},
	"pptx": {
		MIMETypes:  []string{"application/zip", "application/vnd.openxmlformats-officedocument.presentationml.presentation"},
		MagicBytes: [][]byte{{0x50, 0x4B, 0x03, 0x04}}, // ZIP
	},
	// 音频文件
	"mp3": {
		MIMETypes: []string{"audio/mpeg"},
		MagicBytes: [][]byte{
			{0x49, 0x44, 0x33}, // ID3v2 header
			{0xFF, 0xFB},       // MPEG audio frame
		},
	},
	"wav": {
		MIMETypes:  []string{"audio/wav", "audio/x-wav"},
		MagicBytes: [][]byte{{0x52, 0x49, 0x46, 0x46}}, // RIFF header
	},
	"ogg": {
		MIMETypes:  []string{"audio/ogg"},
		MagicBytes: [][]byte{{0x4F, 0x67, 0x67, 0x53}}, // OggS
	},
	"oga": {
		MIMETypes:  []string{"audio/ogg"},
		MagicBytes: [][]byte{{0x4F, 0x67, 0x67, 0x53}}, // OggS
	},
	"flac": {
		MIMETypes:  []string{"audio/flac"},
		MagicBytes: [][]byte{{0x66, 0x4C, 0x61, 0x43}}, // fLaC
	},

	// 视频文件
	"mp4": {
		MIMETypes: []string{"video/mp4"},
		MagicBytes: [][]byte{
			{0x00, 0x00, 0x00, 0x18, 0x66, 0x74, 0x79, 0x70, 0x6D, 0x70, 0x34}, // mp4 file type
			{0x00, 0x00, 0x00, 0x1C, 0x66, 0x74, 0x79, 0x70, 0x6D, 0x70, 0x34}, // mp4 file type
		},
	},
	"mov": {
		MIMETypes:  []string{"video/quicktime"},
		MagicBytes: [][]byte{{0x00, 0x00, 0x00, 0x14, 0x66, 0x74, 0x79, 0x70, 0x71, 0x74, 0x20}}, // QuickTime
	},
	"avi": {
		MIMETypes:  []string{"video/x-msvideo"},
		MagicBytes: [][]byte{{0x52, 0x49, 0x46, 0x46}}, // RIFF header
	},
}

// 所有文件格式规则
var AllExtRules = util.MapMerge(ImageExtRules, FileExtRules)
